package com.atstudy.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2022/12/30 10:43
 * @copyright: 博为峰
 */
public class SeverDemo2 {
    public static void main(String[] args) throws IOException {
        //端口 65535
        //创建服务器端对象
        ServerSocket serverSocket = new ServerSocket(8000);

        while (true) {
            //获取客户端连接
            Socket socket = serverSocket.accept();//阻塞

            //通过Socket获取客户端发来的数据（输入流-读）
            InputStream inputStream = socket.getInputStream();
            byte[] bytes = new byte[1024];
            int len = inputStream.read(bytes);
            System.out.println(socket.getInetAddress().getHostAddress() + ": " + new String(bytes, 0, len));

            //写数据到客户端
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(("欢迎[" + socket.getInetAddress().getHostAddress() + "]的到来").getBytes());
        }
    }

}
